$(function() {
    
      var  Username= $( "#txtUsername" ),
      Password=$("#txtPassword"),
            allFields = $( [] ).add( Username ).add( Password ), url="model.php", tips = $( ".validateTips" );
      function updateTips( t ) {
            tips
                .text( t )
                .addClass( "ui-state-highlight" );
            setTimeout(function() {
                tips.removeClass( "ui-state-highlight", 1500 );
            }, 500 );
        }
        function checkRegexp( o, regexp, n ) {
            if ( !( regexp.test( o.val() ) ) ) {
                o.addClass( "ui-state-error" );
                updateTips( n );
                return false;
            } else {
                return true;
            }
        }
        function checkInputFile( o, n, min ) {
            if (o.val().length < min ) {
                o.addClass( "ui-state-error" );
                updateTips( "Please input " + n + "." );
                return false;
            } else {
                return true;
            }
        }
         function checkLength( o, n, min, max ) {
            if ( o.val().length > max || o.val().length < min ) {
                o.addClass( "ui-state-error" );
                updateTips( "Length of " + n + " must be between " +
                    min + " and " + max + "." );
                return false;
            } else {
                return true;
            }
        }
    $(".btnLogout").click(function(){
         window.location="logout.php";      
    });    
    
    $(".btnLogin").click(function(){
        $( "#dialog-form" ).dialog( "open" );
    });
      $( "#dialog:ui-dialog" ).dialog( "destroy" );
        
          $( "#dialog-form" ).dialog({
            autoOpen: false,
            height: 250,
            width: 350,
            modal: true,
            buttons: { 
                "Login": function() {
                    var bValid = true;
                    allFields.removeClass( "ui-state-error" );

                    bValid = bValid && checkInputFile( Username, "username", 1 );
                    bValid = bValid && checkInputFile( Password, "password", 1 );
                    
                    if (bValid){
                        
                         $.ajax({
                            type: 'POST',
                            cache: false,
                            url: url,
                            data: {'m':'login','u':Username.val(),'p':Password.val()},      
                            success: function(data){
                                  var obj=eval('(' + data + ')');
                                   if (obj['msg']==0){
                                       updateTips('Username & Password ไม่ถูกต้อง');
                                  }else{
                                      $( "#dialog-form" ).dialog( "close" );    
                                        window.location="index.php";          
                                    
                                   }             
                              }
                            });
                    }
                },
                "Register":function(){
                    window.location="register.php";                                            
                } 
            },
            close: function() {
                allFields.val( "" ).removeClass( "ui-state-error" );
            }
        });
});